package com.etlmaster.datax.plugins.tranformer;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.dp.nebula.wormhole.common.interfaces.ILine;
import com.dp.nebula.wormhole.common.interfaces.IParam;
import com.etlmaster.executor.utils.LogWriter;

/**
 * 行级处理 日期格式转换
 * @author wugf
 * @date 2016-04-29
 */
@ComponentAnnotation("timestampChange")
public class TransFieldTimestampFormat extends LineTransProcessor{

	private String sourceFormat;
	private String targetFormat;
	public TransFieldTimestampFormat(Integer fieldIndex, String changeCode,String paramStr,
			IParam param) {
		super(fieldIndex, changeCode,paramStr, param);
	}

	@Override
	public void transField(ILine line,IParam param) {
		String sourceValue = line.getField(getFieldIndex());
		line.replaceField(transTimestampFormat(sourceValue,sourceFormat,targetFormat), getFieldIndex());
	}

	public String transTimestampFormat(String sourceValue,String sourceFormat, String targetFormat) {
		String targetValue=sourceValue;
		SimpleDateFormat sdf;
		try {
			sdf=new SimpleDateFormat(sourceFormat);
			Date date = sdf.parse(sourceValue);
			sdf = new SimpleDateFormat(targetFormat);
			targetValue = sdf.format(date);
		} catch (Exception e) {
			LogWriter.addLog("ERROR","时间格式转换异常,{}",e.getMessage());

		}
		return targetValue;
	}

	@Override
	public boolean isSkip() {
		// TODO Auto-generated method stub
		return false;
	}

}
